"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
from datetime import timedelta, datetime, tzinfo

from django.test import TestCase
from BigPrize.GameCtl.Game import Game
from BigPrize.GameRule.BPTest import BPTest
from BigPrize.models import Draw, VlUser, UserVote, UserWin


class SimpleTest(TestCase):
    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.assertEqual(1 + 1, 2)
        #bp = BPTest("test_ComputeDrawPrz")
        #bp.test_ComputeDrawPrz()



"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
#coding:utf-8
import string
from django.test import TestCase
import time
from BigPrize import models
from BigPrize.GameRule.BP import BigPrize

__author__ = 'zhouqi'

class BPTest(TestCase):
    def test_ComputeDrawPrz(self):
        vote = models.UserVote()
        vote.voteId = 1
        vote.userId = VlUser(id = 1)
        vote.drawId = Draw(id = 1)
        vote.content = string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
        vote.chance = 1
        vote.time = time.time()
        vote.multiple = 1

        wins = []
        for i in range(1, 2):
            win = models.UserWin()
            win.voteId = UserVote(voteId = 1)
            win.userId = VlUser(id = 1)
            win.drawId = Draw(id = 1)
            win.content = string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
            win.chance = 1
            win.time = vote.time
            win.multiple = 1
            win.przLvl = i
            win.winId = str(win.voteId.voteId) + "_" + str(win.przLvl)
            wins.append(win)
        wins[0].winChance = 1
        wins[0].prize = 0

        self.__AssertEqualList(BigPrize.ComputeDrawPrz(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
                                                 , {1: 0, 2: 0, 3: 0, 4: 3000, 5: 600, 6: 100, 7: 10, 8: 5}, [vote, ]), wins)

        vote = models.UserVote()
        vote.voteId = 1
        vote.userId = VlUser(id = 1)
        vote.drawId = Draw(id = 1)
        vote.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 127, 1, 2]), '')
        vote.chance = 1
        vote.time = time.time()
        vote.multiple = 1

        wins = []
        for i in [1, 4]:
            win = models.UserWin()
            win.voteId = UserVote(voteId = 1)
            win.userId = VlUser(id = 1)
            win.drawId = Draw(id = 1)
            win.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 127, 1, 2]), '')
            win.chance = 1
            win.time = vote.time
            win.multiple = 1
            win.przLvl = i
            win.winId = str(win.voteId.voteId) + "_" + str(win.przLvl)
            wins.append(win)
        wins[0].winChance = 1
        wins[0].prize = 0
        wins[1].winChance = 5
        wins[1].prize = 3000

        self.__AssertEqualList(BigPrize.ComputeDrawPrz(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
                                                 , {1: 0, 2: 0, 3: 0, 4: 3000, 5: 600, 6: 100, 7: 10, 8: 5}, [vote, ]), wins)

        vote = models.UserVote()
        vote.voteId = 1
        vote.userId = VlUser(id = 1)
        vote.drawId = Draw(id = 1)
        vote.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 127, 1, 2, 3]), '')
        vote.chance = 1
        vote.time = time.time()
        vote.multiple = 1

        wins = []
        for i in [1, 2, 4, 5]:
            win = models.UserWin()
            win.voteId = UserVote(voteId = 1)
            win.userId = VlUser(id = 1)
            win.drawId = Draw(id = 1)
            win.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 127, 1, 2, 3]), '')
            win.chance = 1
            win.time = vote.time
            win.multiple = 1
            win.przLvl = i
            win.winId = str(win.voteId.voteId) + "_" + str(win.przLvl)
            wins.append(win)
        wins[0].winChance = 1
        wins[0].prize = 0
        wins[1].winChance = 2
        wins[1].prize = 0
        wins[2].winChance = 5
        wins[2].prize = 3000
        wins[3].winChance = 10
        wins[3].prize = 600

        exp = BigPrize.ComputeDrawPrz(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
                                                 , {1: 0, 2: 0, 3: 0, 4: 3000, 5: 600, 6: 100, 7: 10, 8: 5}, [vote, ])
        self.__AssertEqualList(exp, wins)
        


        vote = models.UserVote()
        vote.voteId = 1
        vote.userId = VlUser(id = 1)
        vote.drawId = Draw(id = 1)
        vote.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 127, 1, 2, 3, 4]), '')
        vote.chance = 1
        vote.time = time.time()
        vote.multiple = 1

        wins = []
        for i in range(1, 9):
            win = models.UserWin()
            win.voteId = UserVote(voteId = 1)
            win.userId = VlUser(id = 1)
            win.drawId = Draw(id = 1)
            win.content = string.join(map(chr, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 127, 1, 2, 3, 4]), '')
            win.chance = 1
            win.time = vote.time
            win.multiple = 1
            win.przLvl = i
            win.winId = str(win.voteId.voteId) + "_" + str(win.przLvl)
            wins.append(win)
        wins[0].winChance = 1
        wins[0].prize = 0
        wins[1].winChance = 4
        wins[1].prize = 0
        wins[2].winChance = 1
        wins[2].prize = 0
        wins[3].winChance = 25
        wins[3].prize = 3000
        wins[4].winChance = 100
        wins[4].prize = 600
        wins[5].winChance = 125
        wins[5].prize = 100
        wins[6].winChance = 500
        wins[6].prize = 10
        wins[7].winChance = 526
        wins[7].prize = 5

        self.__AssertEqualList(BigPrize.ComputeDrawPrz(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
                                                 , {1: 0, 2: 0, 3: 0, 4: 3000, 5: 600, 6: 100, 7: 10, 8: 5}, [vote, ]), wins)

        vote = models.UserVote()
        vote.voteId = 1
        vote.userId = VlUser(id = 1)
        vote.drawId = Draw(id = 1)
        vote.content = string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 127, 33, 34, 35, 127, 12]), '')
        vote.chance = 1
        vote.time = time.time()
        vote.multiple = 1

        wins = []
        for i in range(8, 9):
            win = models.UserWin()
            win.voteId = UserVote(voteId = 1)
            win.userId = VlUser(id = 1)
            win.drawId = Draw(id = 1)
            win.content = string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 127, 33, 34, 35, 127, 12]), '')
            win.chance = 1
            win.time = vote.time
            win.multiple = 1
            win.przLvl = i
            win.winId = str(win.voteId.voteId) + "_" + str(win.przLvl)
            wins.append(win)
        wins[0].winChance = 20
        wins[0].prize = 5
        exp = BigPrize.ComputeDrawPrz(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')
                                                 , {1: 0, 2: 0, 3: 0, 4: 3000, 5: 600, 6: 100, 7: 10, 8: 5}, [vote, ])
        self.__AssertEqualList(exp, wins)

    def __AssertEqualList(self, exp, act):
        self.assertEqual(len(exp), len(act))
        for i in range(len(exp)):
            self.assertEqual(exp[i].winChance, act[i].winChance)
            self.assertEqual(exp[i].prize, act[i].prize)
            self.assertEqual(exp[i].winId, act[i].winId)

    def test_Encode(self):
        testDict = (('1,2,3,4,5:1,2', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''))
                    , ('1,2,3,4,5:1,2,3', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3]), ''))
                    , ('1,2,3,4,5:1,2', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''))
                    , ('1,2,3,4,5:1,2,3:6:', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127]), ''))
                    , ('1,2,3,4,5:1,2,3:6:4', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127, 4]), '')))
        for k, v in testDict:
            self.assertEquals(BigPrize.Encode(k), v)

    def test_Decode(self):
        testDict =  (('1,2,3,4,5:1,2', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''))
                    , ('1,2,3,4,5:1,2,3', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3]), ''))
                    , ('1,2,3,4,5:1,2', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''))
                    , ('1,2,3,4,5:1,2,3:6:', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127]), ''))
                    , ('1,2,3,4,5:1,2,3:6:4', string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127, 4]), '')))
        for k, v in testDict:
            self.assertEquals(BigPrize.Decode(v), k)

    def test_Count(self):
        testDict = ((string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''), 1)
                    # multiple
                    , (string.join(map(chr, [1, 2, 3, 4, 5, 6, 127, 1, 2]), ''), 6)
                    , (string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3]), ''), 3)
                    , (string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 127]), ''), 3)
                    # dan & tuo
                    , (string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127]), ''), 15)
                    , (string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127, 4]), ''), 15))
        for k, v in testDict:
            self.assertEquals(BigPrize.Count(k), v)

        errorDict = (string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6]), '')
                     , string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127, 6, 127, 127]), '')
                     , string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 3, 127]), ''))
        for k in errorDict:
            self.assertRaises(Exception, BigPrize.Count, k)

    def test_Find(self):
        self.assertEquals(BigPrize.Find([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])

    def test_GetRightNum(self):
        self.assertEquals(BigPrize.GetRightNum(string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''),
                                               string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), '')), [5, 2, 0, 0])

    def test_CreateDraw(self):

        #self.assertEqual(len(all), 1)
        #self.assertEquals(all[0].id, 1)
        #self.assertEquals(all[0].status, 0)
        game = Game()
        game.PayForDraw(1, string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2]), ''), {1: 0, 2: 0, 3: 0, 4: 3000, 5: 600, 6: 100, 7: 10, 8: 5})
        self.assertEqual(len(UserWin.objects.filter(drawId=1, userId=1)), 2)

    @classmethod
    def setUpClass(cls):
        game = Game()
        game.CreateDraw('00001', 1)
        all = Draw.objects.filter(no='00001', gameId=1)
        BPTest.__AddUser()
        BPTest.__AddUserVote()
        BPTest.__AddUserVote()

    @classmethod
    def __AddUser(cls):
        user = VlUser()
        user.name = "test"
        user.email = "test@gmail.com"
        user.money = 1000
        user.prize = 0
        user.sinaToken = "token"
        user.save()

    @classmethod
    def __AddUserVote(cls):
        vote = UserVote()
        vote.userId = VlUser(id = 1)
        vote.drawId = Draw(id = 1)
        vote.content = string.join(map(chr, [1, 2, 3, 4, 5, 127, 1, 2, 127, 33, 34, 35, 127, 12]), '')
        vote.chance = 1
        vote.time = datetime.now()
        vote.multiple = 1
        vote.save()
